<template>
  <div>
    <!-- 查询表单 -->
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="">
        <el-input v-model="searchForm.name" placeholder="装卸点名称" clearable />
      </el-form-item>

      <el-form-item label="">
        <el-input v-model="searchForm.storeName" placeholder="所属仓库名称" clearable />
      </el-form-item>
      <el-form-item label="">
        <el-input v-model="searchForm.state" placeholder="装卸点状态" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" round @click="search">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 新增 -->
    <div style="display: inline;">
      <el-button type="primary" round @click="handleAdd">新增装卸点</el-button>
      <el-dialog v-model="addDialogVisible" title="新增装卸点信息" width="500">
        <el-form :model="newPosition">
          <el-form-item label="装卸点名称" :label-width="formLabelWidth">
            <el-input v-model="newPosition.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="装卸点代码" :label-width="formLabelWidth">
            <el-input v-model="newPosition.code" autocomplete="off" />
          </el-form-item>
          <el-form-item label="装卸点状态" :label-width="formLabelWidth">
              <el-select v-model="newPosition.state" >
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
          </el-form-item>
          <el-form-item label="所属仓库" :label-width="formLabelWidth">
            <el-select v-model="newPosition.storeName" placeholder="请选择仓库">
              <el-option
                v-for="item in warehouseList"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="addDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="handleAddConfirm">确认</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
    <!-- 导出按钮 -->
    <el-button type="warning" round @click="handleExport">导出数据</el-button>
    <div style="height: 20px;"></div>

    <!-- 数据展示区域 -->
    <el-table :data="tableData.resultList" border style="width: 100%" max-height="500">
      <el-table-column prop="id" sortable label="序号"  />
      <el-table-column prop="name" sortable label="装卸点名称"  />
      <el-table-column prop="code" sortable label="装卸点代码" />
      <el-table-column prop="storeName" sortable label="所属仓库" />
 
      <el-table-column prop="state" sortable label="装卸点状态">
        <template #default="scope">
          <el-tag :type="scope.row.state === '1' ? 'success' : 'info'">
            {{ scope.row.state === '1' ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="createTime" sortable label="创建时间" />
      <el-table-column prop="createName" sortable label="创建人" />
      <el-table-column prop="updateTime" sortable label="修改时间" />
      <el-table-column prop="updateName" sortable label="修改人" />
      
      <!-- 操作列 -->
      <el-table-column fixed="right" label="操作" width="200">
        <template #default="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
            <el-button
              :type="scope.row.state === '1' ? 'info' : 'success'"
              size="small"
              @click="toggleStatus(scope.row)"
            >{{ scope.row.state === '1' ? '禁用' : '启用' }}</el-button>
          
        </template>
      </el-table-column>
    </el-table>

    <!-- 信息修改 -->
    <el-dialog v-model="editDialogVisible" title="编辑装卸点信息" width="500">
      <el-form :model="editForm">
        <el-form-item label="装卸点名称" :label-width="formLabelWidth">
            <el-input v-model="editForm.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="装卸点代码" :label-width="formLabelWidth">
            <el-input v-model="editForm.code" autocomplete="off" />
          </el-form-item>
          <el-form-item label="装卸点状态" :label-width="formLabelWidth">
            <el-input v-model="editForm.state" autocomplete="off" />
          </el-form-item>
          <el-form-item label="所属仓库" :label-width="formLabelWidth">
            <el-select v-model="editForm.storeName" placeholder="请选择仓库">
              <el-option
                v-for="item in warehouseList"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleEditConfirm">确认</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 分页区域 -->
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="searchCondition.pageNum"
        v-model:page-size="searchCondition.pageSize"
        :page-sizes="[5, 10, 15, 20]"
        :size="size"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.totalNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { searchWareHouseName, searchPosition, addPosition, deletePosition, updatePosition} from '../api/api.js'

// ==============================

// 表格数据对象
const tableData = ref(
 [], // 当前页数据列表
)

// 对话框显示状态
const addDialogVisible = ref(false)    // 新增对话框
const editDialogVisible = ref(false)   // 编辑对话框
const formLabelWidth = '120px'         // 表单标签宽度

// 搜索表单数据
const searchForm = reactive({
  name: '',   
  storeName: '',
  state: ''
})

// 新增表单数据
const newPosition = reactive({
  name: '',     
  code: '',      
  storeName: '',         
  state: '',    

})

// 编辑表单数据
const editForm = reactive({
  name: '',     
  code: '',      
  storeName: '',         
  state: '',    
})



const searchCondition = {
  name: '',   
  storeName: '',   
  state: '',
  pageNum: 1,      
  pageSize: 10     
}

// 添加仓库列表数据
const warehouseList = ref([])

// 获取仓库列表
const getWarehouseList = async () => {
  const res = await searchWareHouseName()
  warehouseList.value = res.data.data
}

// ==============================

/**
 * 查询列表
 */
const search = async () => {
  searchCondition.name = searchForm.name
  searchCondition.storeName = searchForm.storeName
  searchCondition.state = searchForm.state

  const res = await searchPosition(searchCondition)
  if(res.data.code === 200) {
    ElMessage.success('查询成功')
  } else {
    ElMessage.error('查询失败：' + res.data.message)
  }
  tableData.value = res.data.data
  console.log(tableData.value)

}

/**
 * 新增员工-打开对话框
 */
const handleAdd = async () => {
  Object.keys(newPosition).forEach(key => {
    newPosition[key] = ''
  })
  newPosition.state = '1'
  await getWarehouseList()
  addDialogVisible.value = true
}



/**
 * 新增
 */
const handleAddConfirm = async () => {
  try {
    await addPosition(newPosition)
    ElMessage.success('添加成功')
    search()
    addDialogVisible.value = false
  } catch (error) {
    ElMessage.error('添加失败：' + error.message)
  }
}

/**
 * 编辑对话框
 */
const handleEdit = async (row) => {
  Object.assign(editForm, row)
  await getWarehouseList()
  editDialogVisible.value = true
}

/**
 * 编辑确认提交
 */
const handleEditConfirm = async () => {
  try {
    await updatePosition(editForm)
    ElMessage.success('修改成功')
    search()
    editDialogVisible.value = false
  } catch (error) {
    ElMessage.error('修改失败：' + error.message)
  }
}

/**
 * 删除
 */
const handleDelete = async (row) => {
  try {
    await deletePosition(row.id)
    ElMessage.success('删除成功')
    search()
  } catch (error) {
    ElMessage.error('删除失败：' + error.message)
  }
}

/**
 * 改变每页显示数量
 */
const handleSizeChange = (val) => {
  
  search()
}

/**
 * 改变当前页码
 */
const handleCurrentChange = (val) => {

  search()
}

//更新状态
const toggleStatus = async (row) => {
  try {
    row.state = row.state === '1' ? '0' : '1'
    await updatePosition(row)
    ElMessage.success('状态更新成功')
    search()
  } catch (error) {
    ElMessage.error('状态更新失败：' + error.message)
    row.state = row.state === '1' ? '0' : '1' // 恢复原状态
  }
}

/**
 * 导出数据
 */
const handleExport = () => {
  // 导出逻辑
}

onMounted(() => {
  search()
})
</script>

<style scoped>
 .demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}

.operation-area {
  margin: 15px 0;
}

.pagination-area {
  margin-top: 20px;
  text-align: right;
}
</style>
